package com.itmyx.base.codegeneration.entrance;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itmyx.base.util.String.StringChangeUtil;
import com.itmyx.base.util.codegeneration.FreemarkerUtil;
import com.itmyx.base.util.codegeneration.TableUtil;
import com.itmyx.base.util.log.Logger;
import com.mysql.fabric.xmlrpc.base.Data;

/**
 * 功能描述 代码生成器主入口
 * 
 * @类型名称 Main
 * @版本 1.0
 * @创建者 myx
 * @创建时间 2016年9月10日 上午5:24:11
 * @版权所有 @2016 ITMYX
 * @修改者 myx
 * @修改时间 2016年9月10日 上午5:24:11
 * @修改描述
 */
public class Main {
	protected Logger logger = Logger.getLogger(this.getClass());
	private static Connection connection = TableUtil.getConnection();

	public static void main(String[] args) throws Exception {
		tojavaForChoose("sys_permission","sys_role","sys_role_permission","sys_user","sys_user_role");  
	}

	/**
	 * 概述：通过表名生成java代码
	 * 
	 * @param tables
	 * @throws Exception
	 */
	public static void tojavaForChoose(String... tables) throws Exception {
		/*
		 * ======================================================================
		 * =======================
		 */
		List<String> tableNames = new ArrayList<>();// 初始化
		if (tables.length == 0) {
			List<Map<String, Object>> allTablesInfo = TableUtil
					.getTablesByCon(connection);
			for (Map<String, Object> map : allTablesInfo) {
				tableNames.add((String) map.get("tableName"));
			}
		} else {
			for (String tableName : tables) {
				tableNames.add(tableName);
			}
		}
		// 遍历输出文件
		for (String tableName : tableNames) {
			List<Map<String, Object>> columnInfoByCon = TableUtil
					.getColumnInfoByCon(connection, tableName);
			String className = StringChangeUtil._ToUpperNextCase(tableName,
					"sys_");// 获取类名
			String classNameFirstLowerCase = StringChangeUtil
					.classNameFirstLowerCase(className);// 类名首字母小写
			String packageName = "com.ganinfo.sys"; // 包名

			System.out.println("生成器变量==================>");
			Map<String, Object> root = new HashMap<String, Object>(); // 创建数据模型
			root.put("packageName", packageName); // 包名
			System.out.println("packageName=" + packageName);

			/* 表相关 */
			root.put("tableName", tableName);
			root.put("className", className); // 类名
			root.put("classNameFirstLowerCase", classNameFirstLowerCase);// 类名(首字母小写)
			System.out.println("tableName=" + tableName);
			System.out.println("className=" + className);
			System.out.println("classNameFirstLowerCase="
					+ classNameFirstLowerCase);

			/* 列相关 */
			root.put("columnInfoByCon", columnInfoByCon); // 属性list集合
			System.out.println("columnInfoByCon=" + columnInfoByCon);

			root.put("systemdate", new Data().toString());

			System.out.println("生成器执行开始===================================");
			/* 生成controller */
			FreemarkerUtil.printFile("controllerTemplate.ftl", root,
					"code/controller/" + className + "Controller.java", "",
					"codegeneration");
			/* 生成service */
			FreemarkerUtil.printFile("serviceTemplate.ftl", root,
					"code/service/" + className + "Service.java", "",
					"codegeneration");
			/* 生成serviceimpl */
			FreemarkerUtil.printFile("serviceImplTemplate.ftl", root,
					"code/service/impl/" + className + "ServiceImpl.java", "",
					"codegeneration");
			/* 生成mapperjava */
			FreemarkerUtil.printFile("mapperTemplate.ftl", root, "code/mapper/"
					+ className + "Mapper.java", "", "codegeneration");
			/* 生成mapper */
			FreemarkerUtil.printFile("mapperMysqlTemplate.ftl", root,
					"code/mapper/" + className + "Mapper.xml", "",
					"codegeneration");
			/* 生成pojo */
			FreemarkerUtil.printFile("entityTemplate.ftl", root, "code/pojo/"
					+ className + ".java", "", "codegeneration");
			System.out.println("生成器执行结束===================================");

		}
	}
}
